﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Messages;
using MedicalCare.Translator;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using StructureMap;

namespace MedicalCare.DbLayer.Adaptors
{
    public class BloodPressureAdaptor : IBloodPressureAdaptor
    {
        private ISession _mcSession;

        public BloodPressureAdaptor()
        {
            _mcSession = ObjectFactory.GetNamedInstance<ISession>("MedicalCareSession");
        }

        public IList<BloodPressureEntity> GetPagingRecords(string orderKey, string orderVal,
                                   int pageIndex, int pageSize, string whereStr)
        {
            try
            {
                //var translator = ObjectFactory.GetInstance<IUserInfoToUserMessage>();
                var query = _mcSession.CreateSQLQuery(@" EXEC BloodPressure_Paging_GET @PAGEINDEX=:PAGEINDEX, @PAGESIZE=:PAGESIZE, @ORDERKEY=:ORDERKEY, @ORDERVAL=:ORDERVAL, @WHERE=:WHERE ")
                            .SetInt32("PAGEINDEX", pageIndex)
                            .SetInt32("PAGESIZE", pageSize)
                            .SetString("ORDERKEY", orderKey)
                            .SetString("ORDERVAL", orderVal)
                            .SetString("WHERE", whereStr)
                            .SetResultTransformer(Transformers.AliasToBean<BloodPressureEntity>());
                var bpList = query.List<BloodPressureEntity>();

                return bpList;
            }
            catch (Exception ex)
            {
                //return false;
            }
            return null;
        }

        public int GetRecordsTotal(string whereStr)
        {
            try
            {
                //var translator = ObjectFactory.GetInstance<IUserInfoToUserMessage>();
                var query = _mcSession.CreateSQLQuery(@" EXEC BloodPressure_Paging_Total @WHERE=:WHERE ")
                         .SetString("WHERE", whereStr)
                          .SetResultTransformer(Transformers.AliasToBean<TotalEntity>());

                var result = query.List<TotalEntity>().FirstOrDefault().Total;

                return result;
            }
            catch (Exception ex)
            {
                //return false;
            }
            return 0;
        }

        public int Update(BloodPressureMessage msg)
        {
            return 0;
        }

        public void Remove(BloodPressureMessage opp)
        {
            throw new NotImplementedException();
        }

        public BloodPressureMessage Read(string id)
        {
            throw new NotImplementedException();
        } 
    }
}
